//
//  XSURLProtocol.m
//  Test
//
//  Created by xueshan1 on 2018/8/13.
//  Copyright © 2018年 xueshan1. All rights reserved.
//

#import "XSURLProtocol.h"

@implementation XSURLProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request{
    
    if ([NSURLProtocol propertyForKey:hasInitKey inRequest:request]) {
        return NO;
    }
    
    
    return YES;
}

//+(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request{
//    NSString *urlStr = request.URL.absoluteString;
//
////    request.URL = [NSURL URLWithString:@"https://api.douban.com/v2/user/:name"];
//
////    NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.douban.com/v2/user/:name"]];
//
//    return request;
//}
+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
    NSMutableURLRequest *mutableReqeust = [request mutableCopy];
    
    // 替换域名
     NSURL *url = [NSURL URLWithString:@"http://123.56.222.140:6477/winehomeapp-user/UserLogin/sendSms"];
    mutableReqeust.URL = url;
    
//    mutableReqeust = [self redirectHostInRequset:mutableReqeust];
    
    return mutableReqeust;
}


-(void)startLoading{
    NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy];
    //做下标记，防止递归调用
    [NSURLProtocol setProperty:@YES forKey:hasInitKey inRequest:mutableReqeust];
    self.connection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self];
}
-(void)stopLoading{
    [self.connection cancel];
}

#pragma mark - NSURLConnectionDelegate

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [self.client URLProtocol:self didFailWithError:error];
}
#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//    self.responseData = [[NSMutableData alloc] init];
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//    [self.responseData appendData:data];
    [self.client URLProtocol:self didLoadData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.client URLProtocolDidFinishLoading:self];
}





@end








